//
//  TTInvalidCouponViewController.m
//  TaoTuo
//
//  Created by admin on 2019/10/14.
//  Copyright © 2019 admin. All rights reserved.
//

#import "TTInvalidCouponViewController.h"
#import "TTCouponViewCell.h"
#import "TTCouponModel.h"

static NSString *cellID = @"invaild_coupon_cell_id";

@interface TTInvalidCouponViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
@property (nonatomic, assign) NSInteger page;

@end

@implementation TTInvalidCouponViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"无效优惠券";
    
    self.page = 1;
    [self getCouponsDatas];
    
    @weakify(self)
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        @strongify(self)
        self.page = 1;
        [self getCouponsDatas];
    }];
    
    self.tableView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        @strongify(self)
        self.page++;
        [self getCouponsDatas];
    }];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    TTCouponModel *model = self.dataArray[indexPath.row];
    TTCouponViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    [cell setModel:model isInvalid:YES];
    cell.backgroundColor = [UIColor clearColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewAutomaticDimension;
}

- (void)getCouponsDatas
{
    NSDictionary *dic = @{
        @"PageIndex" : @(self.page),
        @"PageSize" : @40,
        @"IsRefresh" : @1,
        @"CouponState" : @(2)
    };
    
    @weakify(self)
    [[LLNetworkManagerTool sharedManager] postJsonWithUrlString:getCouponList_api parameters:dic success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        @strongify(self)
        
        if (self.page == 1) {
            [self.dataArray removeAllObjects];
        }
        NSArray *array = responseObject;
        for (NSDictionary *dic in array) {
            TTCouponModel *model = [TTCouponModel modelWithJSON:dic];
            [self.dataArray addObject:model];
        }
        
        [self.tableView.mj_header endRefreshing];
        if (array.count < 40) {
            [self setupFooterView];
            [self.tableView.mj_footer endRefreshingWithNoMoreData];
        } else {
            [self.tableView.mj_footer endRefreshing];
        }
        
        [self.tableView reloadData];
        
        
    } failure:^(NSURLSessionDataTask * _Nonnull task, NSError * _Nullable error) {
        
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        
    } showLoadingInView:self.view];
}

- (void)setupFooterView
{
    UILabel *label = [UILabel labelTextCenterWithTitle:@"没有更多券了" color:UIColorHex(999999) fontSize:12];
    label.frame = CGRectMake(0, 0, self.view.width, 40);
    self.tableView.tableFooterView = label;
}


- (UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
        _tableView.backgroundColor = kSeparatorColor;
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.estimatedRowHeight = 130;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 10);
        [_tableView registerClass:[TTCouponViewCell class] forCellReuseIdentifier:cellID];
        _tableView.tableFooterView = [UIView new];
        [self.view addSubview:_tableView];
        
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) { make.top.mas_equalTo(self.mas_topLayoutGuideTop);
            make.left.right.equalTo(self.view);
            make.bottom.equalTo(self.mas_bottomLayoutGuideBottom);
        }];
    }
    return _tableView;
}



- (NSMutableArray *)dataArray
{
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

@end
